package lab.relaxng.ws.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.thaiopensource.relaxng.translate.Driver;

import lab.relaxng.ws.db.Schema;
import lab.relaxng.ws.util.DBUtils;
import lab.relaxng.ws.util.LoggerOutputStream;

@Path("/instanceToRng")
public class XmlInstanceToRngSchemaResource {
	
	@GET
	@Produces(MediaType.APPLICATION_XML)
	public String convertXmlInstanceToRngSchema(@QueryParam("instance") String xmli) {
		StringBuilder rng = new StringBuilder();
		
		DBUtils.reset();
		LoggerOutputStream.schema = new StringBuilder();
		
		DBUtils.updateSchema(xmli, "");
		
		new Driver().run(new String[] {"-I", "xml", "-O", "rng", xmli, "test.rng"});
		
		return DBUtils.getConversion();
	}

}
